﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cms_vote_result。
    /// </summary>
    public class cms_vote_result
    {
        public cms_vote_result()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int v_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cms_vote_result");
            strSql.Append(" where v_id=" + v_id + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cms_vote_result model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.v_eid != null)
            {
                strSql1.Append("v_eid,");
                strSql2.Append("" + model.v_eid + ",");
            }
            if (model.v_c_id != null)
            {
                strSql1.Append("v_c_id,");
                strSql2.Append("" + model.v_c_id + ",");
            }
            if (model.v_o_id != null)
            {
                strSql1.Append("v_o_id,");
                strSql2.Append("" + model.v_o_id + ",");
            }
            if (model.v_value != null)
            {
                strSql1.Append("v_value,");
                strSql2.Append("'" + model.v_value + "',");
            }
            if (model.v_des != null)
            {
                strSql1.Append("v_des,");
                strSql2.Append("'" + model.v_des + "',");
            }
            strSql.Append("insert into cms_vote_result(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cms_vote_result model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cms_vote_result set ");
            if (model.v_eid != null)
            {
                strSql.Append("v_eid=" + model.v_eid + ",");
            }
            if (model.v_c_id != null)
            {
                strSql.Append("v_c_id=" + model.v_c_id + ",");
            }
            if (model.v_o_id != null)
            {
                strSql.Append("v_o_id=" + model.v_o_id + ",");
            }
            if (model.v_value != null)
            {
                strSql.Append("v_value='" + model.v_value + "',");
            }
            if (model.v_des != null)
            {
                strSql.Append("v_des='" + model.v_des + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where v_id=" + model.v_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int v_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cms_vote_result ");
            strSql.Append(" where v_id=" + v_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cms_vote_result GetModel(int v_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" v_id,v_eid,v_c_id,v_o_id,v_value,v_des ");
            strSql.Append(" from cms_vote_result ");
            strSql.Append(" where v_id=" + v_id + " ");
            Usas.Model.Cms.cms_vote_result model = new Usas.Model.Cms.cms_vote_result();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["v_id"].ToString() != "")
                {
                    model.v_id = int.Parse(ds.Tables[0].Rows[0]["v_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["v_eid"].ToString() != "")
                {
                    model.v_eid = int.Parse(ds.Tables[0].Rows[0]["v_eid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["v_c_id"].ToString() != "")
                {
                    model.v_c_id = int.Parse(ds.Tables[0].Rows[0]["v_c_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["v_o_id"].ToString() != "")
                {
                    model.v_o_id = int.Parse(ds.Tables[0].Rows[0]["v_o_id"].ToString());
                }
                model.v_value = ds.Tables[0].Rows[0]["v_value"].ToString();
                model.v_des = ds.Tables[0].Rows[0]["v_des"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select v_id,v_eid,v_c_id,v_o_id,v_value,v_des ");
            strSql.Append(" FROM cms_vote_result ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" v_id,v_eid,v_c_id,v_o_id,v_value,v_des ");
            strSql.Append(" FROM cms_vote_result ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}

